
/**
 * 会员中心地址控制器
 */
var MemberAddressController = (function () {
    function MemberAddressController(context, addressServices, addressAction) {
        var _this = this;
        this.addressServices = addressServices;
        this.addressServices.addressAction = addressAction;
        this._context = context;
        $(this._context).on('click', function (e) {
            e = e || window.event;
            var target = e.target, $_target = $(target);
            if ($_target.hasClass('add-new-address')) {
                _this.addressServices.openDialog($('#edit-cont'), '新增收货地址');
            }
            else if ($_target.hasClass('edit-address')) {
                addressAction.context = $_target.parents('.address-box');
                var attr = $_target.attr('data'), data = $.parseJSON(attr);
                _this.addressServices.openDialog($('#edit-cont'), '编辑收货地址', data);
            }
            else if ($_target.hasClass('del-address')) {
                addressAction.context = $_target.parents('.address-box');
                _this.addressServices.delAddress(parseInt($_target.attr('data-id')));
            }
            else if ($_target.hasClass('set-default')) {
                addressAction.context = $_target.parents('.address-box');
                _this.addressServices.setDefault(parseInt($_target.attr('data-id')));
            }
        });
    }
    return MemberAddressController;
})();
/**
 * 地址操作后DOM事件
 */
var AddressEvent = (function () {
    function AddressEvent() {
    }
    AddressEvent.prototype.add = function (data) {
        var html = template('address-item', data);
        $('#member-address').append(html);
    };
    AddressEvent.prototype.del = function () {
        this.context.remove();
    };
    AddressEvent.prototype.setDefault = function () {
        $('.address-box.default').removeClass('default');
        $('.list-header').after(this.context.addClass('default'));
    };
    AddressEvent.prototype.edit = function (data) {
        var t = 'name phone category address'.split(' '), _this = this;
        t.forEach(function (key) {
            $('.' + key, _this.context).text(data[key]);
        });
        $('.edit-address', this.context).attr('data', data.data);
    };
    return AddressEvent;
})();

var MemberSafeController = (function () {
    function MemberSafeController(oDialog, context) {
        this._box = null;
        this.captchaUrl = '';
        this._phone = '';
        this._timer = null;
        var _this = this;
        this._context = context;
        this.dialog = oDialog;
        $('.bind-phone-btn').click(function () {
            if (_this._box)
                return false;
            _this._box = _this.dialog.dialogElem({
                content: _this._context,
                title: '绑定手机号码',
                cancelValue: '取消',
                okValue: '绑定',
                cancel: function () {
                    _this._destory();
                },
                ok: function () {
                }
            });
        });
        this.bindGetCaptchaBtn();
    }
    MemberSafeController.prototype._destory = function () {
        if (this._timer) {
            clearInterval(this._timer);
            $('.captcha-btn', this._context).removeClass('disabled').text('获取验证码');
        }
        this._box = null;
        this._emptyError();
    };
    MemberSafeController.prototype._emptyError = function () {
        $('.error-msg', this._context).text('');
    };
    MemberSafeController.prototype._setPhoneError = function (msg) {
        $('.phone-error-msg', this._context).text(msg);
    };
    MemberSafeController.prototype.bindGetCaptchaBtn = function () {
        var _this = this;
        $('.captcha-btn', this._context).on('click', function () {
            if ($(this).hasClass('disabled'))
                return false;
            _this._emptyError();
            //验证手机号码
            var sPhone = $('.phone', _this.context).val();
            if (!/^(13\d|145|147|15[0-3]|15[5-9]|177|18[0-3]|18[5-9])\d{8}$/.test(sPhone)) {
                _this._setPhoneError('请输入正确的手机号码');
                return false;
            }
            _this._phone = sPhone;
            _this._sendCaptcha();
        });
    };
    MemberSafeController.prototype._sendCaptcha = function () {
        //TODO 请求发送手机验证码
        var _this = this;
        $.post(this.captchaUrl, { phone: this._phone }, function (data) {
            //TODO 成功 _this.sendSuccess()
            if (data.type) {
                _this._sendSuccess();
            }
            else {
                _this._sendError();
            }
        }, 'json');
        this._sendSuccess();
    };
    /**
     * 发送成功
     */
    MemberSafeController.prototype._sendSuccess = function () {
        $('.captcha-btn', this._context).addClass('disabled');
        var s = "{{time}}秒后重新发送", time = 120, _this = this;
        $('.captcha-btn', this._context).text(s.replace('{{time}}', time));
        this.timer = setInterval(function () {
            if (time == 0) {
                clearInterval(_this.timer);
                _this.timer = null;
                $('.captcha-btn', _this._context).removeClass('disabled').text('重新发送验证码');
                return false;
            }
            $('.captcha-btn', this._context).text(s.replace('{{time}}', --time));
        }, 1000);
    };
    /**
     * 发送失败
     */
    MemberSafeController.prototype._sendError = function (msg) {
        this._setPhoneError(msg);
    };
    return MemberSafeController;
})();




$(function () {
    /**
     * 在会员中心地址修改下启用
     * @param {[type]}
     */
    if ($('#member-address').length) {
        var oAddress = new AddressModule.Address(oDialog);
        oAddress.cityUrl = base + '/resource/plug/citySelect/city.min.js';
        oAddress.addUrl = base + '/member/saveReceiver.jhtml';
        oAddress.saveUrl = base + '/member/saveReceiver.jhtml';
        oAddress.delUrl = base + '/member/deleteReceiver.jhtml';
        oAddress.setDefaultUrl = base + '/member/setDefaultReceiver.jhtml';
        new MemberAddressController($('#member-address'), oAddress, new AddressEvent());
    }

    /**
     * 账号安全
     * @param {[type]}
     */
    if ($('#member-safe').length) {
        new MemberSafeController(oDialog, $('.bind-phone'));
    }
});
